/**
 * @file unified_launcher.cpp
 * @brief Unified launcher for both console and GUI modes
 */

#include <wx/wx.h>
#include <wx/utils.h>
#include <iostream>
#include <string>

// 声明聊天窗口函数
int StartChatWindow(int argc, char* argv[]);

void ShowConsoleHelp() {
    std::cout << "🚀 Unified AI Communication System" << std::endl;
    std::cout << "==================================" << std::endl;
    std::cout << "Usage:" << std::endl;
    std::cout << "  ai_launcher [mode] [options]" << std::endl;
    std::cout << std::endl;
    std::cout << "Modes:" << std::endl;
    std::cout << "  --console, -c     Console interactive mode" << std::endl;
    std::cout << "  --gui, -g         Graphical chat window" << std::endl;
    std::cout << "  --status, -s      System status" << std::endl;
    std::cout << "  --help, -h        This help message" << std::endl;
    std::cout << std::endl;
    std::cout << "Examples:" << std::endl;
    std::cout << "  ai_launcher --gui          # Start chat window" << std::endl;
    std::cout << "  ai_launcher --console      # Start console mode" << std::endl;
    std::cout << "  ai_launcher --status       # Show status" << std::endl;
}

class SimpleConsoleSystem {
public:
    void Start() {
        std::cout << "🚀 AI Console Mode" << std::endl;
        std::cout << "==================" << std::endl;
        std::cout << "Type 'gui' to open chat window, 'exit' to quit" << std::endl;
        
        char buffer[256];
        while (true) {
            std::cout << "console> ";
            std::flush(std::cout);
            
            if (!std::cin.getline(buffer, sizeof(buffer))) {
                break;
            }
            
            std::string input(buffer);
            if (input == "exit" || input == "quit") {
                break;
            }
            else if (input == "gui") {
                std::cout << "Opening chat window..." << std::endl;
                // 这里可以启动 GUI 模式
                std::cout << "Note: Run './ai_launcher --gui' to start chat window" << std::endl;
            }
            else if (input == "help") {
                std::cout << "Console Commands:" << std::endl;
                std::cout << "  gui    - Open chat window" << std::endl;
                std::cout << "  status - System status" << std::endl;
                std::cout << "  help   - This help" << std::endl;
                std::cout << "  exit   - Exit console" << std::endl;
            }
            else if (input == "status") {
                std::cout << "System Status:" << std::endl;
                std::cout << "  Mode: Console" << std::endl;
                std::cout << "  Chat Window: Available (use 'gui' command)" << std::endl;
                std::cout << "  Directory: " << (const char*)wxGetCwd().mb_str() << std::endl;
            }
            else if (!input.empty()) {
                std::cout << "Processing: " << input << std::endl;
            }
        }
    }
};

int main(int argc, char* argv[]) {
    wxInitialize();
    
    if (argc < 2) {
        ShowConsoleHelp();
        wxUninitialize();
        return 1;
    }
    
    std::string mode = argv[1];
    
    if (mode == "--gui" || mode == "-g") {
        std::cout << "Starting AI Chat Window..." << std::endl;
        // 这里应该启动 GUI 应用
        std::cout << "Please run: ./ai_chat_window.exe" << std::endl;
    }
    else if (mode == "--console" || mode == "-c") {
        SimpleConsoleSystem console;
        console.Start();
    }
    else if (mode == "--status" || mode == "-s") {
        std::cout << "Unified AI System Status:" << std::endl;
        std::cout << "  Console Mode: Available" << std::endl;
        std::cout << "  GUI Chat Window: Available" << std::endl;
        std::cout << "  Three Terminal Communication: Enabled" << std::endl;
        std::cout << "  Current Directory: " << (const char*)wxGetCwd().mb_str() << std::endl;
    }
    else if (mode == "--help" || mode == "-h") {
        ShowConsoleHelp();
    }
    else {
        std::cout << "Unknown mode: " << mode << std::endl;
        ShowConsoleHelp();
    }
    
    wxUninitialize();
    return 0;
}
